//
//  AiRoleItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/4/1.
//

import SwiftUI
import Kingfisher

/**
 *  ai 聊天记录
 */
struct ChatRecordItemView: View {
    @State var delTips = false
    @StateObject var viewModel = AiRoleItemViewModel()
    @State private var showContent = false
    @State private var select = false
    
    var bean:ChatRecordBean
    var recordModel : ChatRecordViewModel

    var body: some View {
        // 目前contextMenu 会失效，先不处理，后续看下有没有更好的解决方案
        contentView
            .contentShape(Rectangle())
            .onTapGesture {
                var param = ChatView.Param()
                param.bean = bean.role
                param.isMain = false
                pathManager.jump(type: JumpType.CHAT_VIEW , data: param)
            }
            .contextMenu {
                commonMenuContent
            }
    }
    
    
}

//内容试图
extension ChatRecordItemView {
    private var contentView: some View {
        HStack {
            AvatarImageView(url: bean.role.avatar ?? "", size: 50)
            VStack(alignment:.leading,spacing: 5){
                HStack(spacing: 5) {
                    Text(bean.role.name ?? "")
                        .font(.system(size: 14, weight: .medium))
                        .foregroundColor(.bgNormalText)
                        .lineLimit(1)
                    if bean.role.isSystem == true { //显示官方标签
                        Text("官方").font(.system(size: 8)).foregroundColor(Color.white).lineLimit(2)
                            .padding(.horizontal, 2)
                            .padding(.vertical, 1)
                            .background(themeManager.themeColor)
                            .clipShape(RoundedRectangle(cornerRadius: 3))
                    }
                    Spacer()
                    let timeStr = TimeUtils.getTimeStr(time: bean.chat?.time ?? 0)
                    if !timeStr.isEmpty {
                        Text(timeStr)
                            .font(.system(size: 10))
                            .foregroundColor(.bgNormalText4)
                    }
                    
                }
                Text(bean.getContentStr())
                    .font(.system(size: 12))
                    .foregroundColor(bean.read() ? .bgNormalText4 : .bgUnread)
                    .lineLimit(1)
            }
        }.frame(maxWidth: .infinity,alignment:.leading)
            .padding(EdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0))
            .contentShape(Rectangle())
            .onAppear{
                
            }
    }
}

//菜单
extension ChatRecordItemView {
    // 将菜单内容抽离为一个计算属性，方便复用
    private var commonMenuContent: some View {
        Group {
            Button("查看"){
                showContent.toggle()
            }
            Button("个人资料"){
                pathManager.jump(type: JumpType.AI_ROLE, data: bean.role)
            }
            if bean.aiRoleOften == bean.role.objectId {
                Button("取消常聊"){
                    viewModel.doSetOften(roleId: nil)
                }
            } else {
                Button("设置常聊") {
                    viewModel.doSetOften(roleId: bean.role.objectId)
                }
            }
           
            Button("不显示该聊天") {
                recordModel.doHideChatRecord(bean: bean.role)
            }
        }
    }
    
}

#Preview {
    ChatRecordItemView(bean:ChatRecordBean(role: AiRoleBean()), recordModel: ChatRecordViewModel())
}
